﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using Sfd.Entities;
using Sfd.Services;
using SfdClient.Messaging;

namespace SfdClient.ViewModel
{
    public class MainWindowViewModel : WorkingViewModelBase
    {
        private AzureUser _user;

        #region ShowAccountEditorViewCommand

        private ICommand _showAccountEditorViewCommand;

        public ICommand ShowAccountEditorViewCommand
        {
            get
            {
                if (_showAccountEditorViewCommand == null)
                    _showAccountEditorViewCommand = new RelayCommand(ExecuteShowAccountEditorView, CanExecuteShowAccountEditorView);
                return _showAccountEditorViewCommand;
            }
        }

        public void ExecuteShowAccountEditorView()
        {
            if (!CanExecuteShowAccountEditorView())
                return;

            var message = new NavigateToMessage(ApplicationView.AccountEditorView);

            Messenger.Default.Send(message);
        }

        public bool CanExecuteShowAccountEditorView()
        {
            return true;
        }
        #endregion

        #region DeleteCommand

        private ICommand _deleteCommand;

        public ICommand DeleteCommand
        {
            get
            {
                if (_deleteCommand == null)
                    _deleteCommand = new RelayCommand(ExecuteDeleteCommand, CanExecuteDeleteCommand);
                return _deleteCommand;
            }
        }

        public void ExecuteDeleteCommand()
        {
            if (!CanExecuteDeleteCommand())
                return;


            //if (_user.Projects.Remove(SelectedProject.Project))
            //{
            //    Messenger.Default.Send(new SyncProjectCollectionChangedMessage(this, this){Action = CollectionChangedAction.Delete, Project = SelectedProject.Project});
            //}

        }

        public bool CanExecuteDeleteCommand()
        {
            return true;
            //return SelectedProject != null;
        }

        #endregion

        /// <summary>
        /// Réception d'un message indiquant que la collection des projets a changée
        /// </summary>
        /// <param name="message"></param>
        private void SyncProjectCollectionChanged(SyncProjectCollectionChangedMessage message)
        {
            // chargement du user actuel
            var service = new AzureUserService();
            var user = service.LoadUser();

            // application des modifications
            switch (message.Action)
            {
                case CollectionChangedAction.Add:
                    user.Projects.Add(message.Project);
                    break;
                case CollectionChangedAction.Delete:
                    var deleteProject =
                        user.Projects.Where(prj => prj.ContainerName == message.Project.ContainerName).FirstOrDefault();
                    if (deleteProject != null)
                        user.Projects.Remove(deleteProject);
                    break;
                case CollectionChangedAction.Update:
                    for (int index = 0; index < user.Projects.Count; index++)
                    {
                        if (user.Projects[index].ContainerName == message.Project.ContainerName)
                        {
                            user.Projects[index] = message.Project;
                            break;
                        }
                    }
                    break;
            }
            // sauvegarde
            service.Save(user);
            _user = user;

            // re binding pour mise à jour de la liste
            //Projects = new ObservableCollection<AzureSyncProjectViewModel>(user.Projects.Select(prj => new AzureSyncProjectViewModel(prj)));
        }

    }
}
